package prototype;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-07-26 19:46
 * @notify
 * @version 1.0
 */

import java.io.*;

public class Person implements Cloneable, Serializable {
    private String name;
    private Integer age;
    private Address address;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address=" + address +
                '}';
    }

    public Person deepClone() {
        ObjectOutputStream oo = null;
        ObjectInputStream oi = null;
        try {
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            //写入 对象
            oo = new ObjectOutputStream(bo);
            //写入对象
            oo.writeObject(this);

            ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
            oi = new ObjectInputStream(bi);

            return (Person) oi.readObject();

        } catch (Exception e) {
            System.out.println(e);
        } finally {
            try {
                if (oo != null) {
                    oo.close();
                }
                if (oi != null) {
                    oi.close();
                }
            } catch (Exception e) {

            }
        }
        return null;
    }
}
